package com.tgpms.vforms.controller;

import com.tgpms.auth.security.TokenService;
import com.tgpms.common.Result;
import com.tgpms.exception.*;
import com.tgpms.util.Common;
import com.tgpms.vforms.entity.Person;
import com.tgpms.vforms.service.PersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@Api(value = "PersonController", tags = "D4012 人员信息")
@RestController
@RequestMapping(value = "/ev/vform/person")
public class PersonController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private PersonService personService;

    @Resource
    private TokenService tokenService;

	@ApiOperation(value = "查询人员信息", notes = "查询人员信息", httpMethod = "POST")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "division",value = "机构/内部单位/业主单位/监理单位/施工单位",required = false,defaultValue = ""),
        @ApiImplicitParam(name = "department",value = "处室",required = false,defaultValue = ""),
        @ApiImplicitParam(name = "entityItem",value = "人员编码",required = false,defaultValue = ""),
        @ApiImplicitParam(name = "company",value = "公司",required = false,defaultValue = ""),
    	@ApiImplicitParam(name = "pageNo",value = "页数",required = true,defaultValue = "1"),
    	@ApiImplicitParam(name = "pageSize",value = "每页数量",required = true,defaultValue = "10"),
    	@ApiImplicitParam(name = "dataowner",value = "数据拥有者",required = false),
    	@ApiImplicitParam(name = "po_no",value = "合同号",required = false)
    })
    @PostMapping(value = "/selPersonList")
    public Result<Object> selPersonList(
            @RequestParam(value = "division", required = false, defaultValue = "") String division,
            @RequestParam(value = "department", required = false, defaultValue = "") String department,
            @RequestParam(value = "entityItem", required = false, defaultValue = "") String entityItem,
            @RequestParam(value = "company", required = false, defaultValue = "") String company,
    		@RequestParam(value = "pageNo", required = true,defaultValue = "1") String pageNo,
    		@RequestParam(value = "pageSize", required = true,defaultValue = "10") String pageSize,
    		@RequestParam(value = "dataowner", required = false) String dataowner,
    		@RequestParam(value = "po_no", required = false) String po_no) {
        Result<Object> result = new Result<Object>();
        if(Common.isEmpty(pageNo) || Common.isEmpty(pageSize)) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
            return result;
        }
        try {
            result = personService.selPersonList(po_no,division, department, entityItem, company, pageNo, pageSize, dataowner);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_QUERY.getExplain());
            throw new QueryException(ExceptionExplain.ERROR_BY_QUERY.getExplain());
        }
        return result;
    }

    @ApiOperation(value = "同飞查询人员信息", notes = "同飞查询人员信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "division",value = "机构/内部单位/业主单位/监理单位/施工单位",required = false,defaultValue = ""),
            @ApiImplicitParam(name = "department",value = "处室",required = false,defaultValue = ""),
            @ApiImplicitParam(name = "entityItem",value = "人员编码",required = false,defaultValue = ""),
            @ApiImplicitParam(name = "company",value = "公司",required = false,defaultValue = ""),
            @ApiImplicitParam(name = "pageNo",value = "页数",required = true,defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "每页数量",required = true,defaultValue = "10"),
            @ApiImplicitParam(name = "dataowner",value = "数据拥有者",required = true),
            @ApiImplicitParam(name = "po_no",value = "合同号",required = false)
    })
    @PostMapping(value = "/tfSelPersonList")
    public Result<Object> tfSelPersonList(
            @RequestParam(value = "division", required = false, defaultValue = "") String division,
            @RequestParam(value = "department", required = false, defaultValue = "") String department,
            @RequestParam(value = "entityItem", required = false, defaultValue = "") String entityItem,
            @RequestParam(value = "company", required = false, defaultValue = "") String company,
            @RequestParam(value = "pageNo", required = true,defaultValue = "1") String pageNo,
            @RequestParam(value = "pageSize", required = true,defaultValue = "10") String pageSize,
            @RequestParam(value = "dataowner", required = false) String dataowner,
            @RequestParam(value = "po_no", required = false) String po_no) {
        Result<Object> result = new Result<Object>();
        if(Common.isEmpty(pageNo) || Common.isEmpty(pageSize)) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
            return result;
        }
        try {
            result = personService.tfSelPersonList(po_no,division, department, entityItem, company, pageNo, pageSize, dataowner);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_QUERY.getExplain());
            throw new QueryException(ExceptionExplain.ERROR_BY_QUERY.getExplain());
        }
        return result;
    }

    /**
     *  @Author: Riven on 2020-8-10 15:02
     *  @param:
     *  @return:
     */
    @ApiOperation(value = "添加人员接口信息", notes = "添加人员接口信息", httpMethod = "POST")
    @PostMapping(value = "/addPersonInfo")
    public Result addPersonInfo(@RequestBody Person person, HttpServletRequest request) {
        Result result = new Result();
        try {
            //获取token操作人
            person.setInsertUser(tokenService.getUserName(request));
            person.setInsertDate(new Date());
            Map<String, String> map = personService.addPersonInfo(person);
            if(map.get("code").equals("1")) {
                result.setSuccess(true);
                result.setData(map.get("data"));
            } else {
                result.setSuccess(false);
            }
            result.setMsg(map.get("info"));
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
            throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
        }
        return result;
    }

    /**
     *  @Author: Riven on 2020-8-10 15:02
     *  @param:
     *  @return:
     */
    @ApiOperation(value = "更新人员接口信息", notes = "更新人员接口信息", httpMethod = "PUT")
    @PutMapping(value = "/updatePersonInfo")
    public Result updatePersonInfo(@RequestBody Person person, HttpServletRequest request) {
        Result result = new Result();
        if(Common.isEmpty(person.getEntityItem())) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
            return result;
        }
        try {
            //获取token操作人
            person.setModifyUser(tokenService.getUserName(request));
            person.setModifyDate(new Date());
            Map<String, String> map = personService.updatePersonInfo(person);
            if (map.get("code").equals("1")) {
                result.setSuccess(true);
            } else {
                result.setSuccess(false);
            }
            result.setMsg(map.get("info"));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
            throw new UpdateException(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
        }
        return result;
    }

    /**
     *  @Author: Riven on 2020-8-10 15:02
     *  @param:
     *  @return:
     */
    @ApiOperation(value = "删除人员接口信息", notes = "删除人员接口信息", httpMethod = "DELETE")
    @ApiImplicitParam(name = "entityItem",value = "人员编码",required = true)
    @DeleteMapping(value = "/deletePersonInfo")
    public Result deletePersonInfo(@RequestParam(value = "entityItem", required = true) String entityItem) {
        Result result = new Result();
        if(Common.isEmpty(entityItem)) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
            return result;
        }
        try {
            Map<String, String> map = personService.deletePersonInfo(entityItem);
            if(map.get("code").equals("1")) {
                result.setSuccess(true);
            }else {
                result.setSuccess(false);
            }
            result.setMsg(map.get("info"));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
            throw new DeleteException(ExceptionExplain.ERROR_BY_DELETE.getExplain());
        }
        return result;
    }
}
